package com.suishoupai.xmb.entity;


/**
 * Created by caojian on 2017/3/23.
 */
public class AjaxMessage {
    private static final int CODE_SUCCESS_HINT = 2;// 提醒
    private static final int CODE_OK = 1;
    private static final int CODE_FAIL = -1;
    private static final int CODE_LOGIN_LOSE = -2;// 登录失效
    private static final int CODE_ERROR_HINT = -3;
    private static final int CODE_ERROR_VERIFICATION = -4;
    private int code;
    private Object data;
    private String errorMsg;

    private AjaxMessage(int code) {
        if (code != CODE_SUCCESS_HINT && code != CODE_OK && code != CODE_FAIL && code != CODE_LOGIN_LOSE
                && code != CODE_ERROR_HINT && code != CODE_ERROR_VERIFICATION) {
            throw new IllegalArgumentException("code非法！");
        }
        this.code = code;
    }

    public static AjaxMessage newErrorMsg(String errorMsg) {
        AjaxMessage message = new AjaxMessage(CODE_FAIL);
        message.errorMsg = errorMsg;
        return message;
    }

    public static AjaxMessage newErrorVerificationMsg(String errorMsg) {
        AjaxMessage message = new AjaxMessage(CODE_ERROR_VERIFICATION);
        message.errorMsg = errorMsg;
        return message;
    }

    public static AjaxMessage newErrorHintMsg(String errorMsg) {
        AjaxMessage message = new AjaxMessage(CODE_ERROR_HINT);
        message.errorMsg = errorMsg;
        return message;
    }

    public static AjaxMessage newSuccessHintMsg(String errorMsg) {
        AjaxMessage message = new AjaxMessage(CODE_SUCCESS_HINT);
        message.errorMsg = errorMsg;
        return message;
    }

    public static AjaxMessage newSuccessHintMsg(String errorMsg, Object data) {
        AjaxMessage message = new AjaxMessage(CODE_SUCCESS_HINT);
        message.errorMsg = errorMsg;
        message.data = data;
        return message;
    }

    public static AjaxMessage newSuccessMsg() {
        AjaxMessage message = new AjaxMessage(CODE_OK);
        return message;
    }

    public static AjaxMessage newSuccessMsg(Object data) {
        AjaxMessage message = new AjaxMessage(CODE_OK);
        message.data = data;
        return message;
    }

    public int getCode() {
        return code;
    }

    public Object getData() {
        return data;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

}
